package com.duan.fragmenttest1;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mineText;
    private TextView musicClubText;
    private TextView findText;
    private ViewPager viewPager;
    private List<Fragment> list;
    private FragmentAdapter fragmentAdapter;
    private Button menu;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.hide();
        //drawerLayout = findViewById(R.id.drawerlayout_main);
        menu = findViewById(R.id.menu);
        mineText = findViewById(R.id.mine);
        musicClubText = findViewById(R.id.music_club);
        findText = findViewById(R.id.find);
        viewPager = findViewById(R.id.main_viewpager);
        menu.setOnClickListener(this);
        mineText.setOnClickListener(this);
        musicClubText.setOnClickListener(this);
        findText.setOnClickListener(this);

        viewPager.setOnPageChangeListener(new MyPagerChangeListener());
        list = new ArrayList<>();//添加Fragment
        list.add(new Mine());//0页面
        list.add(new MusicClub());//1页面
        list.add(new Find());//2页面
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), list);//创建适配器
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setCurrentItem(0);//打开时，默认当前页面为0页面
        mineText.setTypeface(Typeface.DEFAULT_BOLD);//将标题栏对应的字体设置为粗体
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mine:
                viewPager.setCurrentItem(0);
                mineText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                musicClubText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                findText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                break;
            case R.id.music_club:
                viewPager.setCurrentItem(1);
                mineText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                musicClubText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                findText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                break;
            case R.id.find:
                viewPager.setCurrentItem(2);
                mineText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                musicClubText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                findText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            case R.id.menu:
                drawerLayout.openDrawer(GravityCompat.START);

        }

    }

    class MyPagerChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {//滑动监听
                case 0:
                    mineText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    musicClubText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    findText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    break;
                case 1:
                    mineText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    musicClubText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    findText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    break;
                case 2:
                    mineText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    musicClubText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    findText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}